package com.tyros.storage;

import com.tyros.web.mybatis.MybatisAutoConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;

/**
 * @author zhangshuaiyin
 * @date 2022-2-19 21:18:29
 */
@Slf4j
@SpringBootApplication(scanBasePackages = "com.tyros", exclude = MybatisAutoConfiguration.class)
public class StorageApplication {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(StorageApplication.class);
        // initEventListeners(springApplication);
        springApplication.run(args);
    }

    /**
     * spring/springboot 事件监听
     *
     * @param application spring 容器
     */
    @SuppressWarnings("unused")
    private static void initEventListeners(SpringApplication application) {
        application.addListeners(
                // log不输出，out可以输出，该事件初始化时可能还没有加载日志框架
                (ApplicationStartingEvent event) -> System.out.println(event.getClass().getName()),
                (ApplicationListener<ApplicationEnvironmentPreparedEvent>) StorageApplication::logEvent,
                (ApplicationListener<ApplicationContextInitializedEvent>) StorageApplication::logEvent,
                (ApplicationListener<ApplicationPreparedEvent>) StorageApplication::logEvent,
                (ApplicationListener<ContextRefreshedEvent>) StorageApplication::logEvent,
                (ApplicationListener<ApplicationStartedEvent>) StorageApplication::logEvent,
                (ApplicationListener<ApplicationReadyEvent>) StorageApplication::logEvent,
                (ApplicationListener<ContextClosedEvent>) StorageApplication::logEvent,
                (ApplicationListener<ApplicationFailedEvent>) StorageApplication::logEvent,
                // 未输出
                (ApplicationListener<ContextStartedEvent>) StorageApplication::logEvent,
                (ApplicationListener<ContextStoppedEvent>) StorageApplication::logEvent
        );
    }

    private static void logEvent(ApplicationEvent event) {
        log.info("# {} ###############", event.getClass().getSimpleName());
    }

}
